<?php

/**
 * 用在MemcacheList模式中的迭代器
 * 当缓存中的数据丢失时，从MYSQL取出数据，在取数据的同时就把数据放回到MEMCACHE中了
 * 只能用在以下类中：
 * Pattern_Memcache_List
 *
 * @author ananda
 */
class Pattern_Memcache_RebuildCache extends Data_Collection
{
    protected $conn;
    protected $parent;
    protected $key_name;

    public function __construct($handler, Pattern_Memcache $parent, $key_name)
    {
        parent::__construct($handler);
        $this->parent   = $parent;
        $this->conn     = $this->parent->getCacheConnection();
        $this->key_name = $key_name;
    }

    public function init() {}

    public function rowCount()
    {
        return $this->handler->rowCount();
    }

    public function getArray()
    {
        return $this->handler->fetchAll();
    }

    /**
     * 迭代器接口
     * @return mixed
     */
    public function current()
    {
        $row = $this->handler->current();
        $this->conn->update($this->parent->generateKey($row[$this->parent->{$this->key_name}]), $row);
        return $row;
    }

    /**
     * 迭代器接口
     * @return mixed
     */
    public function key()
    {
        return $this->handler->key();
    }

    /**
     * 迭代器接口
     * @return void
     */
    public function next()
    {
        $this->handler->next();
    }

    /**
     * 迭代器接口
     * @return void
     */
    public function rewind()
    {
        $this->handler->rewind();
    }

    /**
     * 迭代器接口
     * @return boolean
     */
    public function valid()
    {
        return $this->handler->valid();
    }
}
